package com.lambda.hybrid.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.TextUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Util {
    private static final char[] CHAR = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String md5(String text) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] data = digest.digest(text.getBytes());

            char[] buff = new char[data.length * 2];
            for (int i = 0; i < data.length; i += 1) {
                buff[i * 2] = CHAR[(data[i] >> 4) & 0xf];
                buff[i * 2 + 1] = CHAR[data[i] & 0xf];
            }

            text = new String(buff);
        } catch (NoSuchAlgorithmException e) {/**/}

        return text;
    }

    public static int getResId(Context context, String name) {
        Resources resources = context.getResources();
        return resources.getIdentifier(name, "drawable", context.getPackageName());
    }

    public static int getColor(String str) {
        int color = 0;
        if (!TextUtils.isEmpty(str)) {
            if (str.length() == 4) {
                str = str + str.substring(1);
            }

            try {
                color = Color.parseColor(str);
            } catch (IllegalArgumentException e) {
                color = 0;
            }
        }
        return color;
    }

    public static int getStatusBarHeight(Resources resources) {
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height;
        try {
            height = resources.getDimensionPixelSize(resourceId);
        } catch (Exception e) {
            height = 0;
        }
        return height;
    }
}
